package org.apache.database.execute;

import org.apache.database.pool.DatabaseConnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BaseSqlExecute extends Execute {

    @Override
    public ResultSet executeForResult(String sql) {
        DatabaseConnection databaseConnection = null;
        Statement statement = null;
        try {
            databaseConnection = getConnectionPool().getConnect();

            statement = databaseConnection.getConnection().createStatement();

            ResultSet resultSet = statement.executeQuery(sql);

            return resultSet;

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseConnect(databaseConnection);
        }
        return EMPTY;
    }

    @Override
    public int execute(String sql) {
        DatabaseConnection databaseConnection = null;
        Statement statement = null;
        try {
            databaseConnection = getConnectionPool().getConnect();
            Connection connection = databaseConnection.getConnection();
            statement = connection.createStatement();

            return statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                ;
            }
            releaseConnect(databaseConnection);
        }
        return -1;
    }
}
